<?php

if (file_exists(APPPATH.'models/basemodel.php')) include_once(APPPATH.'models/basemodel.php');

class ContactActionModel extends BaseModel {

	public function ContactActionModel() {
		parent::BaseModel();
		$this->setModel('contactaction');
		$this->obj =& get_instance();
		$this->load->database();
	}
	
	public function getNextXDaysActionsForUser($startdate = 'now()', $days = 7, $userid = 0) {
		$sql = "SELECT A.*, C.firstname, C.surname, C2.company_name, C2.id as companyid
				FROM contactaction A INNER JOIN company_contact CC ON (A.contactid = CC.contactid)
					INNER JOIN contact C ON (CC.contactid = C.id)
					INNER JOIN company C2 ON (CC.companyid = C2.id)
				WHERE DateDiff(A.actiondate, now() ) <= ".intval($days) ." 
					AND A.ownerid = ".intval($userid)." 
					AND A.completed = 0
					AND A.deleted = 0";
		
		$rs = $this->db->query($sql)->result();
		
		if (count($rs)) return $rs;
		else return false;
		
	}
	
	public function getMissedActionsFor($userid = 0) {
		$sql = "SELECT A.*, C.firstname, C.surname, C2.company_name, C2.id as companyid
				FROM contactaction A INNER JOIN company_contact CC ON (A.contactid = CC.contactid)
					INNER JOIN contact C ON (CC.contactid = C.id)
					INNER JOIN company C2 ON (CC.companyid = C2.id)
				WHERE A.actiondate < NOW() 
					AND A.ownerid = ".intval($userid)." 
					AND A.completed = 0
					AND A.deleted = 0";
		
		$sql .=	" ORDER BY A.actiondate, A.action_title DESC";

		$rs = $this->db->query($sql)->result();
		
		if (count($rs)) return $rs;
		else return false;
	}
	
	public function find($filters = null, $start = null, $count = null, $orderby = null) {
		$this->table_record_count = $this->db->count_all( $this->db_table );

		// Filter could be an array or filter values or an SQL string.
		$where_clause = '';
		if ($filters) {
			if ( is_string($filters) ) {
				$where_clause = $filters;
			}
			elseif ( is_array($filters) ) {
				// Build your filter rules
				if ( count($filters) > 0 ) {
					foreach ($filters as $field => $value) {
						if (is_array($value)) $filter_list[] = " T.$field IN (".implode(',', $value).") ";
						else if (substr($field, 0, 3) == 'md5') {
							$fieldparts = explode('|', $field);
							$filter_list[] = " md5(T.".$fieldparts[1].") = ".$this->db->escape($value)." ";
						} else $filter_list[] = " T.$field = '$value' ";
					}
					$where_clause = ' WHERE ' . join(' AND ', $filter_list );
				}
			}

		}
		
		$order_clause = '';
		if ($orderby) {
			if ( is_string($orderby) ) {
				$order_clause = " ORDER BY " . $orderby;
			} elseif ( is_array($orderby) ) {
				if ( count($orderby) > 0 ) {
					foreach ($orderby as $field => $value) {
						$orderby_list[] = " $field " .$this->db->escape($value) . " ";
					}
					$order_clause = " ORDER BY " . join(' , ', $orderby_list );
				}
			}
		}
		
		$limit_clause = '';
		if ($count) {
			if ($count && $start) {
				$limit_clause = " LIMIT $start, $count ";
			}
			else {
				$limit_clause = " LIMIT $count ";
			}
		}

		// Build up the SQL query string and run the query
		$sql = "SELECT T.*, F.friendly_url, U.firstname AS owner_firstname, U.lastname as owner_lastname
				FROM ".$this->db_table." T LEFT JOIN friendly_urls F ON (T.id = F.contentid AND F.content_type = ".$this->db->escape($this->db_table).")
					LEFT JOIN users U ON (U.id = T.ownerid)
				". $where_clause . $order_clause . $limit_clause;

		$rs = $this->db->query($sql)->result();

		if (count($rs)) return $rs;
		else return false;

	}
	
}

?>